package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.Community;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dean on 2017/8/10.
 */
@Repository
public class CommunityDaoImpl extends BaseDaoImpl<Community> {

    /**
     * 更新
     *
     * @param info
     * @return
     */
    public int editCommunity(Community info) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_community set update_time = now(),updater = ? ");
        arrList.add(info.getUpdater());
        if (!StringUtil.isEmpty(info.getContent())) {
            sb.append(",content = ? ");
            arrList.add(info.getContent());
        }
        if (!StringUtil.isEmpty(info.getTitle())) {
            sb.append(",title = ? ");
            arrList.add(info.getTitle());
        }
        if (info.getPosition() != null) {
            sb.append(",position = ? ");
            arrList.add(info.getPosition());
        }
        if (info.getPublish_status() != null) {
            sb.append(",publish_status = ? ");
            arrList.add(info.getPublish_status());
        }
        if (info.getPublish_time() != null) {
            sb.append(",publish_time = ? ");
            arrList.add(info.getPublish_time());
        }
        if (info.getCommunity_type() != null) {
            sb.append(",community_type = ? ");
            arrList.add(info.getCommunity_type());
        }
        if (info.getChannel_id() != null) {
            sb.append(",channel_id = ? ");
            arrList.add(info.getChannel_id());
        }
        if (info.getColumn_id() != null) {
            sb.append(",column_id = ? ");
            arrList.add(info.getColumn_id());
        }
        if (info.getIs_prize() != null) {
            sb.append(",is_prize = ? ");
            arrList.add(info.getIs_prize());
        }
        if (!StringUtil.isEmpty(info.getPrize())) {
            sb.append(",prize = ? ");
            arrList.add(info.getPrize());
        }
        if (!StringUtil.isEmpty(info.getPic())) {
            sb.append(",pic = ? ");
            arrList.add(info.getPic());
        }
        if (!StringUtil.isEmpty(info.getPk_pic1())) {
            sb.append(",pk_pic1 = ? ");
            arrList.add(info.getPk_pic1());
        }
        if (!StringUtil.isEmpty(info.getPk_pic2())) {
            sb.append(",pk_pic2 = ? ");
            arrList.add(info.getPk_pic2());
        }
        if (!StringUtil.isEmpty(info.getPk_content1())) {
            sb.append(",pk_content1 = ? ");
            arrList.add(info.getPk_content1());
        }
        if (!StringUtil.isEmpty(info.getPk_content2())) {
            sb.append(",pk_content2 = ? ");
            arrList.add(info.getPk_content2());
        }
        sb.append(" where id = ?");
        arrList.add(info.getId());
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int del(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_community set is_del = 1 where id = ? ");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 评论开关
     *
     * @param id
     * @return
     */
    public int toggleComment(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("update t_community set is_comment = 1 - is_comment where id = ? ");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 获取列表
     *
     * @param pageBean
     * @return
     */
    public PageBean list(PageBean pageBean, Community info, boolean is_all) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" SELECT tin.*,tl.name label_name,tu.unit_name,u.name creator_name,tc.channel_name channel_name,tco.column_name column_name " +
                " FROM " +
                " t_community tin " +
                " LEFT JOIN t_label tl ON tl.id = tin.label_id " +
                " LEFT JOIN t_unit tu ON tin.unit_id = tu.id" +
                " LEFT JOIN t_user u ON tin.creator = u.id" +
                " LEFT JOIN t_channel tc ON tin.channel_id = tc.id" +
                " LEFT JOIN t_column tco ON tin.column_id = tco.id" +
                " where tin.is_del = 0 ");
        if (info.getPosition() != null) {
            sb.append(" and tin.position = ? ");
            arrList.add(info.getPosition());
        }
        if (info.getIs_comment() != null) {
            sb.append(" and tin.is_comment = ? ");
            arrList.add(info.getIs_comment());
        }
        if (info.getPublish_status() != null) {
            sb.append(" and tin.publish_status = ? ");
            arrList.add(info.getPublish_status());
        }
        if (info.getLabel_id() != null) {
            sb.append(" and tin.label_id = ? ");
            arrList.add(info.getLabel_id());
        }
        if (info.getChannel_id() != null) {
            sb.append(" and tin.channel_id = ? ");
            arrList.add(info.getChannel_id());
        }
        if (info.getStartTime() != null && !info.getStartTime().replace(" ", "").equals("")) {
            sb.append(" and tin.create_time > ? ");
            arrList.add(info.getStartTime());
        }
        if (info.getEndTime() != null && !info.getEndTime().replace(" ", "").equals("")) {
            sb.append(" and tin.create_time < ? ");
            arrList.add(info.getEndTime());
        }
        if (!StringUtil.isEmpty(info.getTitle())) {
            sb.append(" and tin.title like concat('%',?,'%') ");
            arrList.add(info.getTitle());
        }
        if(!is_all){
            dataScopeFilter(UserUtils.getUser().getId(), sb, arrList, "tu", "tin");
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by tin.id desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 改变阅读数
     *
     * @return
     */
    public int updateReading(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" update t_community set reading = reading + 1 where id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 改变评论数
     * @param id
     */
    public void updateCommentNum(Integer id, Integer num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_community SET comment_num = comment_num + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }
}
